﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Business.Entity;
using Business.Dal;
using System.Collections.Generic;
using System.IO;
using Sahibinden.UserControl;

namespace Sahibinden.UserAction
{
    public partial class IlanGuncelle : System.Web.UI.Page
    {

        Ilan ilan;
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO : Kişi sadece kendi ilanlarını editleyebilir
            //TODO : Adminler bütün ilanları editleyebilir
            if (Request.QueryString.Get("IlanID") == null)
            {
                Response.End();
            }

            string str = Request.QueryString.Get("IlanID").ToString();
                IlanProvider p = new IlanProvider();
                ilan = p.GetIlanByIlanID(Convert.ToInt32(str));
            if (!Page.IsPostBack)
            {
                HerseyiDoldur();
            }

        }
        public void HerseyiDoldur()
        {
            //txtAciklama.Text = ilan.Aciklama;
            txtBaslik.Text = ilan.IlanBasligi;
            txtFiyat.Text = ilan.Fiyat.ToString();

            UlkeDoldur();
            SehirDoldur(ilan.UlkeID);
            IlceDoldur(ilan.SehirID);
            ParaDoldur();
            MahalleDoldur(ilan.IlceID);

            ResimDoldur(ilan.IlanID);
        }

        public void ResimDoldur(int IlanID)
        {
            ResimProvider rp = new ResimProvider();
            List<Resim> lstResim = rp.ResimGetirIlanID(IlanID);

            DataList1.DataSource = lstResim;
            DataList1.DataBind();
        }

        private void ParaDoldur()
        {
            ParaProvider ParaSagla = new ParaProvider();
            drpCinsi.DataTextField = "ParaCinsi";
            drpCinsi.DataValueField = "ParaCinsiID";
            drpCinsi.DataSource = ParaSagla.GetAllRows();
            drpCinsi.DataBind();
        }




        private void UlkeDoldur()
        {
            UlkeProvider UlkeSagla = new UlkeProvider();
            drpUlke.DataTextField = "UlkeAdi";
            drpUlke.DataValueField = "UlkeID";
            List<Ulke> lstUlke =   UlkeSagla.GetAllRows();
            drpUlke.DataSource = lstUlke;
            drpUlke.SelectedIndex = -1;
            drpUlke.DataBind();
            drpUlke.Items.Insert(0, "Bir ülke seçiniz");

            int count = 0;
            foreach (Ulke item in lstUlke)
            {
                count++;
                if (item.UlkeID == ilan.UlkeID)
                {
                    break;
                }
                
            }
            drpUlke.SelectedIndex = count;

        }
        public void SehirDoldur(int UlkeID)
        {
            SehirProvider SehirSagla = new SehirProvider();
            drpSehir.DataTextField = "SehirAdi";
            drpSehir.DataValueField = "SehirID";
            List<Sehirler> lstSehirler = SehirSagla.SehirGetir(UlkeID);
            drpSehir.DataSource = lstSehirler;
            drpSehir.DataBind();

            int count = -1;
            foreach (Sehirler item in lstSehirler)
            {
                count++;
                if (item.SehirID == ilan.SehirID)
                {
                    break;
                }

            }
            drpSehir.SelectedIndex = count;
        }

        public void IlceDoldur(int SehirID)
        {
            IlceProvider ilcesaglayici = new IlceProvider();
            drpIlce.DataTextField = "IlceAdi";
            drpIlce.DataValueField = "IlceID";
            List<Ilceler> lstIlceler = ilcesaglayici.IlceGetir(SehirID);
            drpIlce.DataSource = lstIlceler;//;
            drpIlce.DataBind();

            int count = -1;
            foreach (Ilceler item in lstIlceler)
            {
                count++;
                if (item.IlceID == ilan.IlceID)
                {
                    break;
                }

            }
            drpIlce.SelectedIndex = count;
        }

        public void MahalleDoldur(int IlceID)
        {
            MahalleProvider mahallesagla = new MahalleProvider();
            drpMahalle.DataTextField = "MahalleAdi";
            drpMahalle.DataValueField = "MahalleID";
            List<Mahalleler> lstMahalleler = mahallesagla.MahalleGetir(IlceID);
            drpMahalle.DataSource = lstMahalleler; //(Convert.ToInt32(drpIlce.SelectedValue));
            drpMahalle.DataBind();

            int count = -1;
            foreach (Mahalleler item in lstMahalleler)
            {
                count++;
                if (item.MahalleID == ilan.MahalleID)
                {
                    break;
                }

            }
            drpMahalle.SelectedIndex = count;
        }

        protected void drpSehir_SelectedIndexChanged(object sender, EventArgs e)
        {
            IlceDoldur(Convert.ToInt32(drpSehir.SelectedValue));
        }



        protected void drpIlce_SelectedIndexChanged(object sender, EventArgs e)
        {
            MahalleProvider mahallesagla = new MahalleProvider();
            drpMahalle.DataTextField = "MahalleAdi";
            drpMahalle.DataValueField = "MahalleID";
            drpMahalle.DataSource = mahallesagla.MahalleGetir(Convert.ToInt32(drpIlce.SelectedValue));
            drpMahalle.DataBind();
        }

        protected void drpMahalle_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void drpUlke_SelectedIndexChanged(object sender, EventArgs e)
        {
            SehirDoldur(Convert.ToInt32(drpUlke.SelectedValue));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

           
            ilan.IlanBasligi = txtBaslik.Text;

           // ilan.Aciklama = txtAciklama.Text;
            ilan.UlkeID = Convert.ToInt32(drpUlke.SelectedValue);
            ilan.SehirID = Convert.ToInt32(drpSehir.SelectedValue);
            ilan.MahalleID = Convert.ToInt32(drpMahalle.SelectedValue);
            ilan.IlceID = Convert.ToInt32(drpIlce.SelectedValue);
            ilan.Fiyat = Convert.ToDecimal(txtFiyat.Text);
            ilan.ParaCinsiID = Convert.ToInt32(drpCinsi.SelectedValue);
            ilan.UyeID = Convert.ToInt32(Page.User.Identity.Name);
            IlanProvider ekleyici = new IlanProvider();
            ekleyici.Update(ilan);


            foreach (FileUpload item in pnlFileUpload.Controls.OfType<FileUpload>())
            {
                if (item.FileName == "")
                {
                    continue;
                }
                FileInfo fi = new FileInfo(item.FileName);
                string FileName = Guid.NewGuid().ToString() + fi.Extension;
                item.SaveAs(Server.MapPath("~/UserImages/" + FileName));
                Resim rsm = new Resim();
                rsm.IlanID = ilan.IlanID;
                rsm.ServerFileName = FileName;
                ResimProvider rsmp = new ResimProvider();
                rsmp.Insert(rsm);
            }


            HerseyiDoldur();

        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Sil")
            {
                ResimProvider rp = new ResimProvider();
                rp.Delete(Convert.ToInt32(e.CommandArgument));
                ResimDoldur(ilan.IlanID);
            }
        }



    }
}
